//
//  PageContentView.m
//  YuDou
//
//  Created by yingqi on 2018/6/22.
//  Copyright © 2018年 yingqi. All rights reserved.
//

#import "PageContentView.h"

static NSString *ContentCellID = @"ContentCellID";

@interface PageContentView ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    CGFloat _startOffsetX;
    BOOL _isForbidScrollDelegate;
}

@property (nonatomic, strong) NSArray *childVcs;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation PageContentView

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = self.size;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        //自身属性 滚动指示器 弹 分页
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.bounces = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.frame = self.bounds;
        
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ContentCellID];
    }
    return _collectionView;
}

- (instancetype)initWithFrame:(CGRect)frame childVcs:(NSArray <UIViewController *>*) childVcs parentVc:(UIViewController *)parentVc
{
    self = [super initWithFrame:frame];
    if (self) {
        _isForbidScrollDelegate = NO;
        self.childVcs = childVcs;
        self.parentVc = parentVc;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    for (UIViewController *childVc in self.childVcs) {
        [self.parentVc addChildViewController:childVc];
    }
    [self addSubview:self.collectionView];
}

#pragma mark - CollecetionView DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.childVcs.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ContentCellID forIndexPath:indexPath];
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    UIViewController *childVc = self.childVcs[indexPath.item];
    childVc.view.frame = cell.contentView.bounds;
    [cell.contentView addSubview:childVc.view];
    return cell;
}

#pragma mark - CollecetionView Delegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _isForbidScrollDelegate = NO;
    _startOffsetX = scrollView.contentOffset.x;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_isForbidScrollDelegate) {return;}
    
    //滚动处理
    CGFloat progress   = 0;
    NSInteger sourceIndex = 0;
    NSInteger targetIndex = 0;
    CGFloat currentOffsetX = scrollView.contentOffset.x;
    CGFloat scrollViewW = scrollView.width;
    if (currentOffsetX > _startOffsetX) {
        progress = currentOffsetX/scrollViewW - floor(currentOffsetX/scrollViewW);
        sourceIndex = (NSInteger)(currentOffsetX/scrollViewW);
        targetIndex = sourceIndex + 1;
        if (targetIndex >= self.childVcs.count) {
            targetIndex = self.childVcs.count - 1;
        }
        //完全滑过去
        if (currentOffsetX - _startOffsetX == scrollViewW){
            progress = 1;
            targetIndex = sourceIndex;
        }

    }else{
        progress = 1 - (currentOffsetX/scrollViewW - floor(currentOffsetX/scrollViewW));
        targetIndex =  (NSInteger)(currentOffsetX/scrollViewW);
        sourceIndex = targetIndex + 1;
        if( sourceIndex >= self.childVcs.count){
            sourceIndex = self.childVcs.count - 1;
        }
        //完全划过去
        if (_startOffsetX - currentOffsetX == scrollViewW) {
            sourceIndex = targetIndex;
        }
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(pageContentView:progress:sourceIndex:targetIndex:)]) {
        [_delegate pageContentView:self progress:progress sourceIndex:sourceIndex targetIndex:targetIndex];
    }
}

- (void)setCurrentIndex:(NSInteger)currentIndex
{
    _isForbidScrollDelegate = YES;
    CGFloat offsetX = currentIndex * self.collectionView.width;
    [self.collectionView setContentOffset:CGPointMake(offsetX, 0) animated:NO];

}
@end
